<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:h="livcos.org/ns/cosmos/history"
  xmlns:f="livcos.org/ns/xslt/function"
  exclude-result-prefixes="h xs f">
	
	<xsl:import href="base.xsl"/>
	<xsl:import href="../meta.xsl"/>
	
	<xsl:param name="ref"/>
	<xsl:param name="view"/>
	
	<xsl:variable name="doc" select="doc(concat($ref, '.content'))/*"/>
	<xsl:variable name="history" select="doc(concat($ref, '.history'))/*"/>
	
	<xsl:template name="page_css">
		<link href="{f:caller-uri(resolve-uri('css/styles.css'))}" rel="stylesheet" type="text/css" />
	</xsl:template>
	
	<xsl:template name="page_content">
		<h1>History of <xsl:value-of select="f:object-id(.)"/></h1>
		<table>
			<thead>
				<tr>
					<th>Date</th>
					<th>Actor</th>
					<th>Change(s)</th>
				</tr>
			</thead>
			<tbody>
				<xsl:apply-templates select="$history"/>
			</tbody>
		</table>
	</xsl:template>
	
	<xsl:template match="h:history">
		<xsl:apply-templates select="h:change">
			<xsl:sort select="@time" order="descending"/>
		</xsl:apply-templates>
	</xsl:template>
	
	<xsl:template match="@time">
		<xsl:value-of select="format-dateTime(xs:dateTime(.), '[Y0001]-[M01]-[D01]&#160;[H01]:[m01]:[s01]')"/>
	</xsl:template>
	
	<xsl:template match="h:change">
		<tr>
			<td>
				<xsl:variable name="url">
					<xsl:choose>
						<xsl:when test="contains($view, '?')">
							<xsl:variable name="uri" select="substring-before($view, '?')"/>
							<xsl:value-of select="f:substring-before-last($uri, '.')"/>
							<xsl:text>$</xsl:text>
							<xsl:if test="not(contains(f:substring-after-last($uri, '/'), '$'))">$</xsl:if>
							<xsl:value-of select="@id"/>
							<xsl:text>.</xsl:text>
							<xsl:value-of select="f:substring-after-last($uri, '.')"/>
							<xsl:text>?</xsl:text>
							<xsl:value-of select="substring-after($view, '?')"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="f:substring-before-last($view, '.')"/>
							<xsl:text>$</xsl:text>
							<xsl:if test="not(contains(f:substring-after-last($view, '/'), '$'))">$</xsl:if>
							<xsl:value-of select="@id"/>
							<xsl:text>.</xsl:text>
							<xsl:value-of select="f:substring-after-last($view, '.')"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<a href="{$url}">
					<xsl:apply-templates select="@time"/>
				</a>
			</td>
			<td title="{@actor}"><xsl:value-of select="f:strip-before(@actor, '#')"/></td>
			<td><xsl:apply-templates select="h:*"/></td>
		</tr>
	</xsl:template>
	
	<xsl:template match="text()">
		<div class="row">
			<span class="text">
				<xsl:attribute name="id"><xsl:apply-templates select="." mode="id"/></xsl:attribute>
				<xsl:text>&#160;</xsl:text>
				<xsl:value-of select="."/>
			</span>
		</div>
	</xsl:template>
	
	<xsl:template match="h:*">
		<div>
			<a onclick="Dom(this.parentNode.nextSibling).toggle()">
				<xsl:value-of select="local-name()"/>
				<xsl:text> </xsl:text>
				<xsl:value-of select="@ref"/>
			</a>
		</div>
		<div style="display: none">
			<xsl:apply-templates select="node()"/>
		</div>
	</xsl:template>
	
	<xsl:template match="h:insert">
		<div>
			<a onclick="Dom(this.parentNode.nextSibling).toggle()">Insert <xsl:value-of select="@ref"/></a>
		</div>
		<div style="display: none">
			<xsl:apply-templates select="node()"/>
		</div>
	</xsl:template>
	
	<xsl:template match="h:update">
		<div>
			<a onclick="Dom(this.parentNode.nextSibling).toggle()">Update <xsl:value-of select="@ref"/></a>
		</div>
		<div style="display: none">
			<xsl:apply-templates select="node()"/>
		</div>
	</xsl:template>
	
	<xsl:template match="h:*[not(*) and (normalize-space() = '')]">
	</xsl:template>
	
	<xsl:template match="h:delete">
		<div>
			<a onclick="Dom(this.parentNode.nextSibling).toggle()">Delete <xsl:value-of select="@ref"/></a>
		</div>
		<div style="display: none">
			<xsl:apply-templates select="node()"/>
		</div>
	</xsl:template>
	
</xsl:stylesheet>
